/* 
    需求：
    复制 [data] 文件家中的 创建线程.flv
*/

const fs = require("fs");
const path = require("path");
const process = require("process");

const dirNamePath = path.join(__dirname, "./data/创建线程.flv");
const dirNameCopyPath = path.join(__dirname, "./data/创建线程Copy.flv");

let fileName = "创建线程-流创建.flv";
const dirNameCopyStreamPath = path.join(__dirname, "./data/", fileName);

/* 方式一 */
/* fs.readFile */

// let fileBuff = fs.readFileSync(dirNamePath);
// fs.writeFileSync(dirNameCopyPath, fileBuff)

// let useMemory = process.memoryUsage();
// console.log(useMemory); // 40153088/1024/1024 = 38.29Mb

/* 方式二 */
/* fs.createWriteStream */

let wsReadFile = fs.createReadStream(dirNamePath);
let wsWriteFile = fs.createWriteStream(dirNameCopyStreamPath);
wsReadFile.on("data", (chunk) => {
  wsWriteFile.write(chunk);
});

wsReadFile.on("end", () => {
  console.log("------ 读取完成 ------");
  let useMemory = process.memoryUsage();
  console.log(useMemory); //  41254912, 39.34375 Mb
});
